/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.swing.help;

import java.io.*;

import javax.swing.*;

/**
 * Note: <br>
 * To optimize this class is not only easy, but also urgent.
 * 
 * @author Francisco Morero Peyrona
 */

final class ContextHelp extends JScrollPane
{
    private JEditorPane htmlViewer = new JEditorPane();
    private String sCurrentContext = "";

    //-------------------------------------------------------------------------//

    ContextHelp()
    {
        this.htmlViewer.setContentType( "text/html" );
        this.htmlViewer.setEditable( false );

        setViewportView( this.htmlViewer );
        setContext( "welcome" );
    }

    void setContext( String sFile )
    {
        if( !sFile.equals( this.sCurrentContext ) )
        {
            String sLanguage = new JWindow().getLocale().getLanguage();
            
            try
            {    
                // By default pages are loaded asynchronously: perfect.
                this.htmlViewer.setPage( "file:help/quick/"+ sLanguage +'/'+ sFile +".html" );
            }
            catch( IOException exc )  // Localized help does not exists: then try with English
            {
                try
                {
                    this.htmlViewer.setPage( "file:help/quick/en/"+ sFile +".html" );
                }
                catch( IOException e )
                {
                    this.htmlViewer.setText( "<html><body>File: 'help/quick/"+ sLanguage +'/'+ sFile +
                                  ".html<br> Help file not found</body></html>" );
                }
            }
        }
    }
}